home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / arg.zip / ARG.CPP next >
Text File  |  1991-02-15  |  8KB  |  351 lines

  1. /****************************************************************************\
  2.  
  3.                          Copyright (C) 1991                        
  4.                          Joe Kleinwaechter
  5.  
  6.       This file remains the property of Joe Kleinwaechter and may be
  7.       used freely at no charge. It may not however be resold by anyone or
  8.       included in a Class Library package without my written consent.
  9.  
  10.  
  11.  File:         flag.cpp
  12.  
  13.  Description:  This handles command line arguments and values
  14.         
  15.  Created by:   Joe Kleinwaechter (j2k)
  16.  Date:         14-Feb-1991
  17.  
  18. \****************************************************************************/
  19.  
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <ctype.h>
  24.  
  25. #include "flag.hpp"
  26.  
  27.  
  28. void main(int argc, char *argv[]);
  29.  
  30.  
  31.  
  32.  
  33. /****************************************************************************\
  34.  
  35.  Name:             argString::argString(char *s)
  36.  
  37.  Description:      This is the constructor for the class argString
  38.  
  39.  Parameters:       s - this is the command line string to be passed
  40.  
  41.  Returns:          nothing
  42.  
  43.  Preconditions:    
  44.  
  45.  Altered Global:
  46.  
  47.  Altered Static:
  48.  
  49.  Remarks:          This class is to be only used by the class arg.
  50.  
  51.  Side effects:
  52.  
  53. \****************************************************************************/
  54. argString::argString(char *s)
  55. {
  56.  
  57.    value = 0;  // preset value and linked list pointer
  58.    next = 0;
  59.  
  60.    if (*s == '-')  // parameter turned off or has value
  61.       {
  62.       state = ARG_CLEAR;
  63.       s++;
  64.       if (*s)          // this prevents going past eol
  65.          {
  66.          letter = *s;  // get the switch name
  67.          s++;          // point to beginning of phrase
  68.          }
  69.       }
  70.    else if (*s == '+') // parameter turned on or has value
  71.       {
  72.       state = ARG_SET;
  73.       s++;
  74.       if (*s)          // this prevents going past eol
  75.          {
  76.          letter = *s;  // get the switch name
  77.          s++;          // point to beginning of phrase
  78.          }
  79.       }
  80.    else                // non-switch parameter
  81.       {
  82.       state = ARG_NO_SWITCH;
  83.       letter = 0;
  84.       }
  85.  
  86.    // s now points to the string part of the switch (NULL if there isn't one)
  87.  
  88.    if (s)  // only make a new string if one exists
  89.       value = strdup(s);
  90.  
  91. }
  92.  
  93. /****************************************************************************\
  94.  
  95.  Name:             argString::~argString()
  96.  
  97.  Description:      This is the destructor for the class argString
  98.  
  99.  Parameters:       none
  100.  
  101.  Returns:          nothing
  102.  
  103.  Preconditions:
  104.  
  105.  Altered Global:
  106.  
  107.  Altered Static:
  108.  
  109.  Remarks:         This class is only useable by the class arg.
  110.  
  111.  Side effects:
  112.  
  113. \****************************************************************************/
  114. argString::~argString()
  115. {
  116.    letter = 0;
  117.    if (value)
  118.       free(value);
  119.    next = 0;
  120. }
  121.  
  122.  
  123.  
  124. /****************************************************************************\
  125.  
  126.  Name:             arg::arg(int ac, char *av[], int c)
  127.  
  128.  Description:      This is the constructor for the class arg.
  129.  
  130.  Parameters:       ac - the argc parameter passed to main()
  131.                    av - the argv parameter passed to main()
  132.                    c  - 1 if switches are case-sensitive, else 0
  133.                         This has a default value of being non-case-sensitive
  134.  
  135.  Returns:          nothing
  136.  
  137.  Preconditions:    ac and av must be passed.
  138.  
  139.  Altered Global:
  140.  
  141.  Altered Static:
  142.  
  143.  Remarks:          
  144.  
  145.  Side effects:
  146.  
  147. \****************************************************************************/
  148. arg::arg(int ac, char *av[], int c)
  149. {
  150. int firstTime = 1;
  151. argString *link;
  152.  
  153.    caseSensitive = c;
  154.    first = 0;
  155.  
  156.    for (ac--;ac;ac--)
  157.       {
  158.       if (firstTime)
  159.          {
  160.          first = new argString(av[ac]);
  161.          link = first;
  162.          firstTime = 0;
  163.          }
  164.       else
  165.          {
  166.          link->next = new argString(av[ac]);  // build the object
  167.          link = link->next;                   // point to newly created object
  168.          }
  169.       }
  170.  
  171. }
  172.  
  173.  
  174. /****************************************************************************\
  175.  
  176.  Name:             arg::~arg()
  177.  
  178.  Description:      This is the destructor for the calss arg.
  179.  
  180.  Parameters:     
  181.  
  182.  Returns:        
  183.  
  184.  Preconditions:
  185.  
  186.  Altered Global:
  187.  
  188.  Altered Static:
  189.  
  190.  Remarks:        
  191.  
  192.  Side effects:
  193.  
  194. \****************************************************************************/
  195. arg::~arg()
  196. {
  197. argString *p;
  198. argString *n;
  199.  
  200.    p = first;
  201.  
  202.    while (p)
  203.       {
  204.       n = p->next;
  205.       delete p;
  206.       p = n;
  207.       }
  208.    first = 0;
  209. }
  210.  
  211.  
  212.  
  213. /****************************************************************************\
  214.  
  215.  Name:             argState arg::isSet(char l, char *s)
  216.  
  217.  Description:      See if a certain flag has been set,cleared or not specified.
  218.  
  219.  Parameters:       l - the character flag name. Use 0 for unnamed switches.
  220.                    s - a pointer to a string where the value of the parameter
  221.                        should be placed. The user must allocate enough space
  222.                        for this string.
  223.  
  224.  Returns:         ARG_NOT_SPECIFIED - this switch was not specified by the user
  225.                   ARG_CLEAR - this switch had a leading minus sign (-).
  226.                   ARG_SET,  - this switch had a leading plus sign (+).
  227.                   ARG_NO_SWITCH - this parameter is unlabeled
  228.  
  229.  
  230.  Preconditions:
  231.  
  232.  Altered Global:
  233.  
  234.  Altered Static:
  235.  
  236.  Remarks:        
  237.  
  238.  Side effects:
  239.  
  240. \****************************************************************************/
  241. argState arg::isSet(char l, char *s)
  242. {
  243. argString *p;
  244.  
  245.    p = locate(l);
  246.    if (!p) // no match in the list
  247.       {
  248.       *s = 0;
  249.       return ARG_NOT_SPECIFIED;
  250.       }
  251.  
  252.    strcpy(s,p->value);
  253.    return p->state;
  254.  
  255. }
  256.  
  257. /****************************************************************************\
  258.  
  259.  Name:             argString * arg::locate(char l)
  260.  
  261.  Description:      Find the argString that has letter = l
  262.  
  263.  Parameters:       l - the switch to look for
  264.  
  265.  Returns:          A pointer to the argString that matches. 0 - if none
  266.  
  267.  Preconditions:    
  268.  
  269.  Altered Global:
  270.  
  271.  Altered Static:
  272.  
  273.  Remarks:         This is not accessible to the user.
  274.  
  275.  Side effects:
  276.  
  277. \****************************************************************************/
  278. argString * arg::locate(char l)
  279. {
  280. argString *p;
  281.  
  282.    p = first;
  283.  
  284.    if (!caseSensitive)
  285.       l = toupper(l);
  286.  
  287.    while (p)
  288.       {
  289.  
  290.       if ( (caseSensitive?p->letter:toupper(p->letter)) == l)
  291.          return p;
  292.       else
  293.          p = p->next;
  294.       }
  295. }
  296.  
  297.  
  298. /****************************************************************************\
  299.  
  300.  Name:             void arg::display()
  301.  
  302.  Description:      Display the list of arguments and their conditions to the
  303.                    screen.
  304.  
  305.  Parameters:       none
  306.  
  307.  Returns:          nothing
  308.  
  309.  Preconditions:
  310.  
  311.  Altered Global:
  312.  
  313.  Altered Static:
  314.  
  315.  Remarks:         This is meant for debugging purposes.
  316.  
  317.  Side effects:
  318.  
  319. \****************************************************************************/
  320. void arg::display()
  321. {
  322. argString *p;
  323.  
  324.    p = first;
  325.  
  326.    while (p)
  327.       {
  328.       switch(p->state)
  329.          {
  330.          case ARG_NOT_SPECIFIED:
  331.             printf("l=%c  state=ARG_NOT_SPECIFIED   value=%s\n",p->letter, p->value);
  332.             break;
  333.          case ARG_CLEAR:
  334.             printf("l=%c  state=ARG_CLEAR           value=%s\n",p->letter, p->value);
  335.             break;
  336.          case ARG_SET:
  337.             printf("l=%c  state=ARG_SET             value=%s\n",p->letter, p->value);
  338.             break;
  339.          case ARG_NO_SWITCH:
  340.             printf("l=%c  state=ARG_NO_SWITCH       value=%s\n",p->letter, p->value);
  341.             break;
  342.          };
  343.  
  344.       p = p->next;
  345.       }
  346.    printf("\n");
  347.  
  348. }
  349.  
  350.  
  351.